package com.portal.cwegportal.service;

import com.appcenter.jz.app.CurrentUser;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.jz.unit.ObjectUnit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.portal.cwegportal.input.columnplate.*;
import com.portal.cwegportal.mapper.ColumnPlateMapper;
import com.portal.cwegportal.model.ColumnPlate;
import com.portal.cwegportal.output.columnplate.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

import static org.apache.http.util.TextUtils.isBlank;
/**
 *  创建人:王增立
 *  备注: 栏目
 *  时间: 2019/4/26 14:51
 *  参数: 
 */
@Service
public class ColumnPlateService {
    @Autowired
    ColumnPlateMapper columnPlateMapper;
    @Transactional
    /**
     *  创建人:王增立
     *  备注: 创建栏目
     *  时间: 2019/4/26 14:52
     *  参数: 
     */
    public CreateColumnPlateOutput createColumnPlate(CreateColumnPlateInput input) throws Exception {
        this.IsExistCode(input.getItem().getColumnPlateCode(),null);
        CreateColumnPlateOutput output = new CreateColumnPlateOutput();
        ColumnPlate columnPlate=new ColumnPlate();
        ObjectUnit.Copy(input.getItem(),columnPlate);
        columnPlate.setGmtCreate(new Date());
        columnPlate.setGmtModified(new Date());
        columnPlate.setDeleted(false);
        columnPlate.setCreatorCode(CurrentUser.getUserCode());
        columnPlate.setCreatorId(CurrentUser.getUserId());
        columnPlate.setCreatorName(CurrentUser.getUserName());
        columnPlateMapper.insertSelective(columnPlate);
        output.setItem(columnPlate);
        return output;
    }


/**
 *  创建人:王增立
 *  备注: 编辑栏目
 *  时间: 2019/4/26 14:52
 *  参数: 
 */
    @Transactional
    public EditColumnPlateOutput editColumnPlate (EditColumnPlateInput input) throws Exception{
        this.IsExistCode(input.getItem().getColumnPlateCode(),input.getItem().getId());

        EditColumnPlateOutput output=new EditColumnPlateOutput();
        ColumnPlate oldEntity= columnPlateMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setKeyType(input.getItem().getKeyType());
        oldEntity.setName(input.getItem().getName());
       oldEntity.setSortNo(input.getItem().getSortNo());
       oldEntity.setColumnPlateCode(input.getItem().getColumnPlateCode());
       oldEntity.setGmtModified(new Date());
        columnPlateMapper.updateByPrimaryKey(oldEntity);
        output.setItem(oldEntity);
        return  output;
    }
    /**
     *  创建人:王增立
     *  备注: 判断大于多少个 编辑大于1
     *  时间: 2019/4/26 14:52
     *  参数: 
     */
    private void IsExistCode(String value,Long id){
        Example example = new Example(ColumnPlate.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        criteria.andEqualTo("columnPlateCode",value);
        if(id!=null){
            criteria.andNotEqualTo("id",id);
        }
        if(columnPlateMapper.selectByExample(example).size()>0)
        {
            throw  new ErrorJsonResult("100","类型编码已存在");
        }
    }
/**
 *  创建人:王增立
 *  备注: 删除栏目
 *  时间: 2019/4/26 14:53
 *  参数: 
 */
    public DeleteColumnPlateOutput deleteColumnPlate(DeleteColumnPlateInput input) {
        DeleteColumnPlateOutput output = new DeleteColumnPlateOutput();
        ColumnPlate oldEntity = columnPlateMapper.selectByPrimaryKey(input.getItem().getId());
        oldEntity.setDeleted(true);
        columnPlateMapper.updateByPrimaryKey(oldEntity);
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 获取栏目
 *  时间: 2019/4/26 14:53
 *  参数:
 */
    public GetColumnPlatesOutput getColumnPlates(GetColumnPlatesInput input) {
        GetColumnPlatesOutput output = new GetColumnPlatesOutput();

        //logger.error("log4j2 error!");

        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"sort_no");
        //List<Menus> ls = MenuMapper.SearchMenus(input);
        Example example = new Example(ColumnPlate.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        if(!isBlank(input.getKeyType()))
        {
            criteria.andEqualTo("keyType", input.getKeyType());
        }

        if(!isBlank(input.getKeyword())){
            criteria.andLike("name","%"+input.getKeyword()+"%");
        }

        List<ColumnPlate> ls=columnPlateMapper.selectByExample(example);
        PageInfo<ColumnPlate> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<ColumnPlate>(page.getList(),(int)page.getTotal()));
        return  output;

    }
/**
 *  创建人:王增立
 *  备注: 加载栏目
 *  时间: 2019/4/26 14:53
 *  参数: 
 */
    public PageLoadOutput pageLoad(PageLoadInput input) {
        PageLoadOutput output = new PageLoadOutput();
        output.setItems(getColumnPlates(input).getItems());
        return output;
    }
/**
 *  创建人:王增立
 *  备注: 获取相应类型下的所有栏目
 *  时间: 2019/4/26 15:17
 *  参数: 
 */
    public GetAllColumnPlatesOutput getAllColumnPlates(GetAllColumnPlatesInput input) {
        GetAllColumnPlatesOutput output = new GetAllColumnPlatesOutput();

        Example example = new Example(ColumnPlate.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("deleted",0);
        if(!isBlank(input.getKeyType()))
        {
            criteria.andEqualTo("keyType", input.getKeyType());
        }
        example.orderBy("sortNo");

        List<ColumnPlate> ls=columnPlateMapper.selectByExample(example);
        PageInfo<ColumnPlate> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<ColumnPlate>(page.getList(),(int)page.getTotal()));
        return  output;

    }
}
